
package it.csttech.date;

import java.util.Date;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;

/**
 * OVERVIEW This class defines an object sent as a error response entity
 * 
 * @author marcello.felappi
 * @author marco.dalena
 */

public class ErrorObject {

	/**
	 * Description of the error
	 */
	private String description;

	/**
	 * Server local time of the error
	 */
	@JsonSerialize(using = DateTimeSerializer.class, as = Date.class)
	private Date serverLocalTime;

	/**
	 * Date occurrence of the error
	 */
	private Date occurrence;

	/**
	 * Cause message of the error
	 */
	private String causeMessage;

	/**
	 * Constructor of the class; accept an exception an build the object error
	 * to be sent to the client
	 * 
	 * @param Throwable
	 *            - exception that will be analyzed to construct the correct
	 *            error object
	 */
	public ErrorObject(Throwable exception) {

		causeMessage =
			exception.getCause() != null
				? exception.getCause().getMessage() : "NO CAUSE";
		if (causeMessage.equals(Messages.getString("ErrorCode.wrongDateFormat")))
			description =
				Messages.getString("ErrorCode.wrongDateFormatMessage");
		else if (causeMessage.equals(Messages.getString("ErrorCode.wrongFloatFormat")))
			description =
				Messages.getString("ErrorCode.wrongFloatFormatMessage");
		else
			description = exception.getMessage();

		occurrence = new Date();
		serverLocalTime = new Date();

	}

	/**
	 * Constructor of the class; build the object error to be sent to the client
	 * 
	 * @param String
	 *            description - description of the error
	 * @param Date
	 *            serverLocalTime - server local time of the error occurence
	 * @param Date
	 *            occurrence - date of the error occurence
	 * @param String
	 *            causeMessage - cause message of the error
	 */
	public ErrorObject(
		String description, Date serverLocalTime, Date occurrence,
		String causeMessage) {

		this.description = description;
		this.serverLocalTime = serverLocalTime;
		this.occurrence = occurrence;
		this.causeMessage = causeMessage;
	}

	public String getCauseMessage() {

		return causeMessage;
	}

	public void setCauseMessage(String causeMessage) {

		this.causeMessage = causeMessage;
	}

	public String getDescription() {

		return description;
	}

	public void setDescription(String description) {

		this.description = description;
	}

	public Date getOccurrence() {

		return occurrence;
	}

	public void setOccurrence(Date occurrence) {

		this.occurrence = occurrence;
	}

	@Override
	public String toString() {

		StringBuilder builder = new StringBuilder();
		builder.append("ErrorObject [description=");
		builder.append(description);
		builder.append(", occurrence=");
		builder.append(occurrence);
		builder.append(", causeMessage=");
		builder.append(causeMessage);
		builder.append("]");
		return builder.toString();
	}

}
